﻿Imports Equipment.Core
Imports Equipment.DataAccess
Imports Equipment.Business
Imports Equipment.Model

Namespace Equipment
    <RequiresPageAcces(Page:=PageType.UserRole)> _
    Public Class RoleController
        Inherits System.Web.Mvc.Controller

        Function Index() As ActionResult
            ViewData("Roles") = Role.RetrieveAll()
            ViewData("Site") = New SelectList(SiteDepartment.RetrieveAll(SiteDepartmentType.Site), "ID", "Description")
            ViewData("EntityType") = EntityType.Role
            Return View()
        End Function

#Region "AJAX"
        <HttpPost()> _
        Function SaveRole(ByVal collection As FormCollection) As ActionResult
            Dim siteID As Integer = Integer.Parse(collection("Site"))
            Dim id As Integer = Integer.Parse(collection("currRoleID"))
            Dim desc As String = collection("roleName")
            Dim changeDetails As New List(Of String)
            Dim role As Role
            Dim existingPageAccessList As Dictionary(Of PageType, PageAccess) = Nothing

            If id > 0 Then
                role = role.GetByPrimaryKey(id)
                existingPageAccessList = PageAccess.RetrieveAllByStatusString(role.PageAccess).ToDictionary(Function(pa) pa.Type)
            End If

            Dim pageAccessList As List(Of PageAccess) = PageAccess.RetrieveAll()

            For Each page In pageAccessList
                Dim isGranted As Boolean = Boolean.Parse(CStr(collection(String.Format("cbxPageAccess_{0}", CInt(page.Type)))).Split(",")(0))
                page.Grant = isGranted

                If existingPageAccessList IsNot Nothing Then
                    If page.Grant Then
                        If Not (existingPageAccessList.ContainsKey(page.Type) AndAlso existingPageAccessList(page.Type).Grant) Then
                            changeDetails.Add(String.Format("Added page '{0}'", page.Description))
                        End If
                    Else
                        If (existingPageAccessList.ContainsKey(page.Type) AndAlso existingPageAccessList(page.Type).Grant) Then
                            changeDetails.Add(String.Format("Deleted page '{0}'", page.Description))
                        End If
                    End If
                End If
            Next

            Dim statusString As String = PageAccess.GetStatusStringByList(pageAccessList)
            If id > 0 Then
                If role IsNot Nothing Then
                    Dim oldDesc = role.Description
                    role.Description = desc
                    role.SiteID = siteID
                    role.PageAccess = statusString
                    role.Save()
                    If oldDesc <> desc Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Description", oldDesc, desc))
                    End If
                    ModificationHistory.Insert(EntityType.Role, desc, ModificationAction.Modified, Application.CurrentUserInfo.UserID, changeDetails, role.ID, role.SiteID)
                    NotificationManager.CreateNotificationsByTrigger(NotificationTrigger.UserRole_Modify, oldDesc, role)
                End If
            Else
                Dim newRole As Role = role.Insert(siteID, desc, statusString)
                ModificationHistory.Insert(EntityType.Role, desc, Application.CurrentUserInfo.UserID, newRole.ID, newRole.SiteID)
                NotificationManager.CreateNotificationsByTrigger(NotificationTrigger.UserRole_Create, desc, newRole)
            End If

            ViewData("Roles") = role.RetrieveAll()
            Return PartialView("~/Views/Role/Controls/RoleGrid.ascx")
        End Function

        Function RoleDetail(ByVal id As Integer) As ActionResult
            Dim currRole As Role
            If id > 0 Then
                currRole = Role.GetByPrimaryKey(id)
                ViewData("Site") = New SelectList(SiteDepartment.RetrieveAll(SiteDepartmentType.Site), "ID", "Description", currRole.SiteID)
            Else
                currRole = New Role() With {.ID = 0, .SiteID = 0, .Description = "Enter New Role"}
                ViewData("Site") = New SelectList(SiteDepartment.RetrieveAll(SiteDepartmentType.Site), "ID", "Description")
            End If

            ViewData("CurrentRole") = currRole
            ViewData("Pages") = PageAccess.RetrieveAllByStatusString(currRole.PageAccess)

            Return PartialView("~/Views/Role/Controls/RoleDetail.ascx")
        End Function
#End Region

    End Class
End Namespace